package org.tis.tools.senior.module.developer.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.senior.module.core.web.util.ShiroUtil;
import org.tis.tools.senior.module.developer.controller.request.BranchAddAndUpdateRequest;
import org.tis.tools.senior.module.developer.controller.request.VerifcationUrlRequest;
import org.tis.tools.senior.module.developer.entity.SBranch;
import org.tis.tools.senior.module.developer.entity.SSvnAccount;
import org.tis.tools.senior.module.developer.service.ISBranchService;
import org.tmatesoft.svn.core.SVNException;

import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;

/**
 * sBranch的Controller类
 * 
 * @author Auto Generate Tools
 * @date 2018/06/20
 */
@RestController
@RequestMapping("/sBranch")
@Validated
public class SBranchController extends BaseController<SBranch>  {

    @Autowired
    private ISBranchService sBranchService;

    //@OperateLog(type = OperateType.ADD, desc = "新增分支")
    @ApiOperation(value = "新增分支接口", notes = "body传参")
    @PostMapping
    public ResultVO add(@RequestBody @Validated({BranchAddAndUpdateRequest.add.class,Default.class})
                                    BranchAddAndUpdateRequest request) {
        SSvnAccount user = ShiroUtil.getUser();
        sBranchService.add(user.getUserId(), request);
        return ResultVO.success("新增成功！");
    }

    //@OperateLog(type = OperateType.UPDATE, desc = "修改分支")
    @ApiOperation(value = "修改分支接口", notes = "body传参")
    @PutMapping
    public ResultVO update(@RequestBody @Validated({BranchAddAndUpdateRequest.update.class,Default.class})
                                       BranchAddAndUpdateRequest request) {
        sBranchService.update(request);
        return ResultVO.success("修改成功！");
    }

    //@OperateLog(type = OperateType.DELETE, desc = "删除分支")
    @ApiOperation(value = "分支删除", notes = "body传参")
    @DeleteMapping("/{guid}")
    public ResultVO delete(@PathVariable @NotNull(message = "guid不能为空") Integer guid) {
        sBranchService.deleteBranchAndMapping(guid);
        return ResultVO.success("删除成功");
    }
    
    @GetMapping("/{guid}")
    public ResultVO detail(@PathVariable @NotNull(message = "id不能为空") Integer guid) {
        SBranch sBranch = sBranchService.selectById(guid);
        if (sBranchService == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", sBranch);
    }


    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated SmartPage<SBranch> page) {

        return  ResultVO.success("查询成功", sBranchService.selectPage(getPage(page), getCondition(page)));
    }

    @GetMapping("/notAllot")
    public ResultVO notAllot() {
        return  ResultVO.success("查询成功", sBranchService.selectNotAllotBranch());
    }

    /**
     * 验证是否是合法的svnUrl路径
     * @param request
     * @return
     */
    @PostMapping("/path")
    public ResultVO verificationUrl(@RequestBody @Validated VerifcationUrlRequest request) throws SVNException {

        return  ResultVO.success("验证通过", sBranchService.verificationUrl(request.getSvnUrl()));
    }
}

